package com.th.coupon.seralization;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.th.coupon.entity.CouponTemplate;

import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * 优惠卷模板实体类自定义序列化器
 * @author 涂航
 */
public class CouponTemplateSerialize extends JsonSerializer<CouponTemplate> {


    @Override
    public void serialize(CouponTemplate value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        //开始序列化对象
        gen.writeStartObject();
        gen.writeStringField("id",value.getId().toString());
        gen.writeStringField("name",value.getName());
        gen.writeStringField("logo",value.getLogo());
        gen.writeStringField("desc",value.getDesc());
        gen.writeStringField("category",value.getCategory().getDescription());
        gen.writeStringField("productLine",value.getProductLine().getDescription());
        gen.writeStringField("count",value.getCount().toString());
        gen.writeStringField("createTime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value.getCreateTime()));
        gen.writeStringField("userId",value.getUserId().toString());
        gen.writeStringField("key",value.getKey() + String.format("%04d",value.getId()));
        gen.writeStringField("target",value.getTarget().getDescription());
        gen.writeStringField("rule", JSON.toJSONString(value.getRule()));
        //结束序列化对象
        gen.writeEndObject();
    }
}
